package cn.tedu.zx.order.adapter.controller;

import cn.tedu.zx.common.restful.JsonResult;
import cn.tedu.zx.order.service.BookingService;
import cn.tedu.zx.pojo.order.dto.BookingAddNewDto;
import cn.tedu.zx.pojo.order.dto.BookingPrepareDto;
import cn.tedu.zx.pojo.order.dto.BookingUpdateDto;
import cn.tedu.zx.pojo.order.entity.Booking;
import cn.tedu.zx.pojo.order.vo.BookingDetailsVO;
import cn.tedu.zx.pojo.order.vo.BookingListVO;
import cn.tedu.zx.pojo.order.vo.UserBookingListVO;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/booking")
@Api(tags = "预订订单模块")
public class OrderController {

    @Autowired
    private BookingService bookingService;

    @Autowired
    RocketMQTemplate rocketMQTemplate;

    /**
     * 使用消息队列处理新增预订
     *
     * @param addNewDto
     * @return
     */
    @PostMapping("/addNew")
    @ApiOperation("增加预定")
    @ApiOperationSupport(order = 100)
//    @PreAuthorize("hasAnyAuthority('/booking/insert')")
    public JsonResult addNew(BookingAddNewDto addNewDto) {
        String orderJsonString = JSON.toJSONString(addNewDto);
        log.debug("获取对象{}",orderJsonString);
        Message message = MessageBuilder.withPayload(orderJsonString.getBytes(StandardCharsets.UTF_8)).build();
        rocketMQTemplate.syncSend("booking-topic", message);
        log.debug("发送消息{}",message);
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("删除/取消预定")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAnyAuthority('/booking/delete')")
    public JsonResult delete(Long id) {
        bookingService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改预定")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAnyAuthority('/booking/update')")
    public JsonResult update(BookingUpdateDto updateDto) {
        bookingService.update(updateDto);
        return JsonResult.ok();
    }


    @PostMapping("/setfinish")
    @ApiOperation("修改状态")
    @ApiOperationSupport(order = 302)
    public JsonResult setFinish() {
        bookingService.setFinish();
        return JsonResult.ok("修改完成");
    }

    @PostMapping("/setCancel")
    @ApiOperation("取消订单")
    @ApiOperationSupport(order = 303)
//    @PreAuthorize("hasAnyAuthority('/booking/update')")
    public JsonResult setCancel(Long id) {
        bookingService.setCancel(id);
        return JsonResult.ok("修改完成");
    }

    @GetMapping("/selectDetails")
    @ApiOperation("查询详情")
    @ApiOperationSupport(order = 400)
    @PreAuthorize("hasAnyAuthority('/booking/read')")
    public JsonResult selectDetails(Long id) {
        BookingDetailsVO detailsVO = bookingService.selectDetails(id);
        return JsonResult.ok(detailsVO);
    }

    @GetMapping("/list")
    @ApiOperation("查询列表")
    @ApiOperationSupport(order = 401)
//    @PreAuthorize("hasAnyAuthority('/booking/read')")
    public JsonResult list() {
        List<BookingListVO> list = bookingService.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/prepareList")
    @ApiOperation("根据日期、类别获取订单场地id、日期、时间段")
    @ApiOperationSupport(order = 402)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", required = true, dataType = "String"),
            @ApiImplicitParam(name = "categoryId", required = true, dataType = "Long")
    })
    public JsonResult<List<BookingPrepareDto>> prepareList(@RequestParam String date, @RequestParam Long categoryId) {
        List<BookingPrepareDto> list = bookingService.prepareList(date, categoryId);

        return JsonResult.ok(list);
    }

    @GetMapping("/listByUserId")
    @ApiOperation("查询用户订单列表")
    @ApiOperationSupport(order = 403)
//    @PreAuthorize("hasAnyAuthority('/booking/read')")
    @ApiImplicitParam(name = "userId", required = true, dataType = "Long")
    public JsonResult<List<UserBookingListVO>> listByUserId(Long userId) {
        log.info("用户id："+userId+"");
        List<UserBookingListVO> list = bookingService.listByUserId(userId);
        return JsonResult.ok(list);
    }
}
